﻿namespace MainGame.GameScreens
{
    using System;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    using XGameLibrary.Controls;
    using XGameLibrary.Input;
    using XGameLibrary.ScreenManager;

    public class StartMenuScreen : BaseGameState
    {
        public StartMenuScreen(Game game, GameStateManager manager) :
            base(game, manager)
        {
        }

        private PictureBox BackgroundImage { get; set; }

        private PictureBox LeftArrowImage { get; set; }

        private PictureBox RightArrowImage { get; set; }

        private LinkLabel StartGame { get; set; }

        private LinkLabel LoadGame { get; set; }

        private LinkLabel ExitGame { get; set; }

        private float MaxItemWidth { get; set; }

        protected override void LoadContent()
        {
            base.LoadContent();

            var content = this.GameRef.Content;

            this.BackgroundImage = new PictureBox(
                content.Load<Texture2D>(@"Images\Backgrounds\MainMenuScreen"),
                this.GameRef.ScreenRectangle);
            this.ControlManager.Add(this.BackgroundImage);

            Texture2D arrowTextureLeft = content.Load<Texture2D>(@"GUI\Menu\leftarrowUp");
            Texture2D arrowTextureRight = content.Load<Texture2D>(@"GUI\Menu\rightarrowUp");

            this.LeftArrowImage = new PictureBox(
                arrowTextureLeft,
                new Rectangle(0, 0, arrowTextureLeft.Width, arrowTextureLeft.Height));
            this.RightArrowImage = new PictureBox(
                arrowTextureRight,
                new Rectangle(0, 0, arrowTextureRight.Width, arrowTextureRight.Height));

            ControlManager.Add(this.LeftArrowImage);
            ControlManager.Add(this.RightArrowImage);

            this.StartGame = new LinkLabel();
            this.StartGame.Text = "New Game";
            this.StartGame.Size = this.StartGame.SpriteFont.MeasureString(this.StartGame.Text);
            this.StartGame.Selected += new System.EventHandler(this.MenuItem_Selected);

            this.LoadGame = new LinkLabel();
            this.LoadGame.Text = "Load Game";
            this.LoadGame.Size = this.LoadGame.SpriteFont.MeasureString(this.LoadGame.Text);
            this.LoadGame.TabStop = false;
            this.LoadGame.Color = Color.Gray;
            this.LoadGame.Selected += this.MenuItem_Selected;

            this.ExitGame = new LinkLabel();
            this.ExitGame.Text = "Exit Game";
            this.ExitGame.Size = this.ExitGame.SpriteFont.MeasureString(this.ExitGame.Text);
            this.ExitGame.Selected += this.MenuItem_Selected;

            this.ControlManager.Add(this.StartGame);
            this.ControlManager.Add(this.LoadGame);
            this.ControlManager.Add(this.ExitGame);

            this.ControlManager.NextControl();

            this.ControlManager.FocusChanged += new EventHandler(this.ControlManager_FocusChanged);
            Vector2 position = new Vector2(560, 590);

            foreach (Control ctrl in this.ControlManager)
            {
                if (ctrl is LinkLabel)
                {
                    if (ctrl.Size.X > this.MaxItemWidth)
                    {
                        this.MaxItemWidth = ctrl.Size.X;
                    }

                    ctrl.Position = position;
                    position.Y += ctrl.Size.Y + 5f;
                }
            }

            this.ControlManager_FocusChanged(this.StartGame, null);
        }

        private void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2(
                control.Position.X + this.MaxItemWidth + 10f,
                control.Position.Y);
            this.LeftArrowImage.SetPosition(position);

            position.X -= this.MaxItemWidth + 60f;
            this.RightArrowImage.SetPosition(position);

            Control.Select.Play();
        }

        private void MenuItem_Selected(object sender, EventArgs e)
        {
            if (sender == this.StartGame)
            {
                this.StateManager.PushState(this.GameRef.CharacterGeneratorScreen);
            }

            if (sender == this.LoadGame)
            {
                this.StateManager.PushState(this.GameRef.GamePlayScreen);
            }

            if (sender == this.ExitGame)
            {
                this.GameRef.Exit();
            }

            Control.Press.Play();
        }

        public override void Update(GameTime gameTime)
        {
            this.ControlManager.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            this.GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            this.ControlManager.Draw(this.GameRef.SpriteBatch);

            this.GameRef.SpriteBatch.End();
        }
    }
}
